package org.springblade.common.tool;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import io.micrometer.core.instrument.util.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.nio.charset.Charset;

public class SocketTest {
	public static void main(String[] args) {
		//测试socket 连接
//		try {
//			Socket socket = new Socket();
//			socket.connect(new InetSocketAddress("47.98.126.153",3301),3000);
//			socket.close();
//		}catch (SocketTimeoutException se){
//			System.out.println("连接超时");
//		}catch (IOException e) {
//			e.printStackTrace();
//		}


		//
		JSch jSch = new JSch();
		try {
			Session session = jSch.getSession("root", "47.98.126.153", 22);
			session.setConfig("StrictHostKeyChecking", "no");
			session.setPassword("Zxf#2018");
			session.connect();
			ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
			InputStream in = null;
			try {
				in = channelExec.getInputStream();
			} catch (IOException e) {
				e.printStackTrace();
			}
			channelExec.setCommand("ping 101.132.189.101 -c 2");
			channelExec.setErrStream(System.err);
			channelExec.connect();
			String out = IOUtils.toString(in, Charset.forName("UTF-8"));
			System.out.println(out);
			channelExec.disconnect();
			session.disconnect();
		} catch (JSchException e) {
			e.printStackTrace();
		}
	}
}
